<?php

declare(strict_types=1);
/**
 * This file is part of Hyperf.
 *
 * @link     https://www.hyperf.io
 * @document https://doc.hyperf.io
 * @contact  group@hyperf.io
 * @license  https://github.com/hyperf-cloud/hyperf/blob/master/LICENSE
 */

namespace App\Controller\Api;
use Hyperf\DbConnection\Db;
use Hyperf\HttpServer\Request;
use Hyperf\View\RenderInterface;
use Illuminate\Support\Facades\Hash;
class DriverController
{
    public function login(Request $request){
        //
      	$driver = DB::table('driver')
            ->where('status',0)
            ->where('telephone','=',$request->name)->first();
        if(isset($driver->id)){
            if(Hash::check($request->password,$driver->password)){
                $_SESSION['network'] = 'http://';
                $_SESSION['driver_id'] = $driver->id;
                $_SESSION['driver_name'] = $driver->name;
                $_SESSION['check_pass'] = 1;
                $_SESSION['REMOTE_ADDR'] = $request->header('x-real-ip');
                $_SESSION['SERVER_NAME'] = $request->header('host');
                $token = getToken();
                //var_dump($token);
                redis_set($token,$_SESSION);
                $res['status'] = true;
                $res['code'] = 200;
                $res['msg'] = '';
                $res['data']['id']  = $driver->id;
                $res['data']['username']  = $driver->name;
                $res['data']['work_status']  = $driver->work_status;
                $res['data']['face_url']  = $_SESSION['network'].$_SESSION['SERVER_NAME'].'/face_url/'.$driver->face_url;
                $res['data']['telephone']  = $driver->telephone;
                $res['data']['nikename']  = $driver->nikename;
                $res['data']['images'][0]['name'] = 'car_photo';
                $res['data']['images'][0]['urls']  = $_SESSION['network'].$_SESSION['SERVER_NAME'].'/image/'.$driver->car_photo;

                $res['data']['images'][1]['name'] = 'car_license';
                $res['data']['images'][1]['urls']  = $_SESSION['network'].$_SESSION['SERVER_NAME'].'/image/'.$driver->car_license;

                $res['data']['images'][2]['name'] = 'person_license';
                $res['data']['images'][2]['urls']  = $_SESSION['network'].$_SESSION['SERVER_NAME'].'/image/'.$driver->person_license;
                $res['token'] = $token;
                return json_encode($res,1);
            }else{
               echo '失败';
            }
        }else{
                $res['status'] = false;
                $res['code'] = 400;
                $res['msg'] = '登陆失败，账号或则密码错误';
                $res['data'] = [];
                return json_encode($res,1);
        }
    }


    //上传图片信息
    public function car_info(Request $request){
         $_SESSION = redis_get($request->token);
         var_dump($_SESSION);
        
         if($request->name == "car_photo"){
              $filename = $this->image("car_photo",$request);
              //$user_id = (int)$request->id;
              $bool = DB::table('driver')->where('id',$_SESSION['driver_id'])->update(array('car_photo'=>$filename));
              if($bool){
                    $pic = DB::table('driver')->where('id',$_SESSION['driver_id'])->first();
                    $res['status'] = true;
                    $res['msg'] = '上传成功';
                    $res['data']['images'][0]['name'] = 'car_photo';
                    $res['data']['images'][0]['urls']  = $_SESSION['network'].$_SESSION['SERVER_NAME'].'/image/'.$pic->car_photo;
                    $res['data']['images'][1]['name'] = 'car_license';
                    $res['data']['images'][1]['urls']  = $_SESSION['network'].$_SESSION['SERVER_NAME'].'/image/'.$pic->car_license;
                    $res['data']['images'][2]['name'] = 'person_license';
                    $res['data']['images'][2]['urls']  = $_SESSION['network'].$_SESSION['SERVER_NAME'].'/image/'.$pic->person_license;
                    return json_encode($res,1);
              }else{
                    $res['status'] = false;
                    $res['msg'] = '上传失败';
                    $res['data']  = array();
                    return json_encode($res,1);
              }
         }else if($request->name == "car_license"){
             $filename = $this->image("car_license",$request);
             $bool = DB::table('driver')->where('id',$_SESSION['driver_id'])->update(array('car_license'=>$filename));
              if($bool){
                    $pic = DB::table('driver')->where('id',$_SESSION['driver_id'])->first();
                    $res['status'] = true;
                    $res['msg'] = '上传成功';
                    $res['data']['images'][0]['name'] = 'car_photo';
                    $res['data']['images'][0]['urls']  = $_SESSION['network'].$_SESSION['SERVER_NAME'].'/image/'.$pic->car_photo;
                    $res['data']['images'][1]['name'] = 'car_license';
                    $res['data']['images'][1]['urls']  = $_SESSION['network'].$_SESSION['SERVER_NAME'].'/image/'.$pic->car_license;
                    $res['data']['images'][2]['name'] = 'person_license';
                    $res['data']['images'][2]['urls']  = $_SESSION['network'].$_SESSION['SERVER_NAME'].'/image/'.$pic->person_license;
                    return json_encode($res,1);
              }else{
                    $res['status'] = false;
                    $res['msg'] = '上传失败';
                    $res['data']  =array();
                    return json_encode($res,1);
              }

         }else if($request->name == "person_license"){
             $filename = $this->image("person_license",$request);
             $bool = DB::table('driver')->where('id',$_SESSION['driver_id'])->update(array('person_license'=>$filename));
              if($bool){
                    $pic = DB::table('driver')->where('id',$_SESSION['driver_id'])->first();
                    $res['status'] = true;
                    $res['msg'] = '上传成功';
                    $res['data']['images'][0]['name'] = 'car_photo';
                    $res['data']['images'][0]['urls']  = $_SESSION['network'].$_SESSION['SERVER_NAME'].'/image/'.$pic->car_photo;
                    $res['data']['images'][1]['name'] = 'car_license';
                    $res['data']['images'][1]['urls']  = $_SESSION['network'].$_SESSION['SERVER_NAME'].'/image/'.$pic->car_license;
                    $res['data']['images'][2]['name'] = 'person_license';
                    $res['data']['images'][2]['urls']  = $_SESSION['network'].$_SESSION['SERVER_NAME'].'/image/'.$pic->person_license;
                    return json_encode($res,1);
              }else{
                    $res['status'] = false;
                    $res['msg'] = '上传失败';
                    $res['data']  = array();
                    return json_encode($res,1);
              }

         }else{
            $res['status'] = 401;
            $res['msg'] = '没有信息';
            $res['data']  =array();
            return json_encode($res,1);
         }
    }

    //return string
    public function image($field,$request){
            $file = $request->file($field);//接前台图片
            if($file->clientMediaType() == "image/png" || $file->clientMediaType() == "image/jpeg"){
                $extension = $file->extension();//图片路径
                $name = date('YmdHis',time()).rand(00000000,99999999999).'.'.$extension;
                $file->move("./image",$name);//移动至框架图片文件夹
                return $name;
            }
    }



    public function performan_map(Request $request){
		$_SESSION = redis_get($request->token);
        $order = DB::table('order')
            ->select('id','total_sell','car_team','driver_get_date','arriver_date')
            ->where('driver_id',$_SESSION['driver_id'])
            ->where('driver_status',2)->get();

               $categories = [];
               $series = [];
               $series[0]['name'] = '利润';
               $series[0]['color'] = '#479FF8';
               $car_team = '';
               foreach ($order as $key=>$value){
                        $driver_get_date  = date('Y.m.d',strtotime($value->driver_get_date));
                        $arriver_date  = date('Y.m.d',strtotime($value->arriver_date));
                        $arr = explode('.',$driver_get_date);
                        $arr2 = explode('.',$arriver_date);

                        $categories[] = $arr[1].'.'.$arr[2].'-'.$arr2[1].'.'.$arr2[2];
                        $series[0]['data'][] = $value->total_sell;
                        $car_team = $value->car_team;
               }

           $res['status'] = true;
           $res['code'] = 200;
           $res['msg'] = '';
           $res['data']['num'] = count($order);
           $res['data']['car_team'] = $car_team;
           $res['data']['categories'] = $categories;
           $res['data']['series'] = $series;
           return json_encode($res,1);

    }







     //用户定位
    public function position(Request $request){
    	$_SESSION = redis_get($request->token);
        $user_id = $_SESSION['driver_id'];
        if(isset($request->position)) {
           DB::table('driver')->where('id', $user_id)->update(array('position' => $request->position));
                $res['status'] = true;
                $res['code'] = 200;
                $res['msg'] = '';
                $res['data'] = [];
                return json_encode($res,1);
        }else{
            	$res['status'] = false;
                $res['code'] = 400;
                $res['msg'] = '';
                $res['data'] = [];
                return json_encode($res,1);
        }
    }









    /**
 * 从网上下载图片保存到服务器
 * @param $url 图片网址
 * @param $path 保存到服务器的路径
 */
    function downloadImages($url,$path) {
        $ch = curl_init ($url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_BINARYTRANSFER,1);
        curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,false);
        curl_setopt($ch,CURLOPT_SSL_VERIFYHOST,false); // 绕过ssl
        $img = curl_exec($ch);

        curl_close($ch);
        // $filename = pathinfo($url, PATHINFO_BASENAME); // 获取文件名
        //$image_name就是要保存到什么路径,默认只写文件名的话保存到根目录

        //保存的文件名称用的是链接里面的名称
        $fp = fopen($path,'w');
        fwrite($fp, $img);
        fclose($fp);
    }






}